{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import traceback\n",
    "import socket\n",
    "from opendatatools import futures\n",
    "from datetime import datetime, timedelta\n",
    "from typing import Optional, Sequence, List\n",
    "from rpc.client import RpcClient\n",
    "from rpc.utility import INTERVAL_ADJUSTMENT_MAP\n",
    "from rpc.utility import get_duration, extract_vt_symbol, to_rq_symbol, handle_df, load_json\n",
    "\n",
    "\n",
    "def get_server_setting():\n",
    "    setting = load_json(\"setting.json\")\n",
    "    return setting\n",
    "\n",
    "\n",
    "def init_client(host: str, port: int, authkey: bytes):\n",
    "    client = RpcClient(host, port, authkey)\n",
    "    client.connect()\n",
    "    return client\n",
    "\n",
    "\n",
    "def get_update_symbol(client: RpcClient) -> List:\n",
    "    symbols = client.get_update_symbol()\n",
    "    print(\"待更新的合约列表获取成功：\")\n",
    "    print(symbols)\n",
    "    return symbols\n",
    "\n",
    "\n",
    "def query_by_symbol(vt_symbol: str, source_interval: str) -> dict:\n",
    "    # symbol convert rules of opendatatools is same with rqdata\n",
    "    # opendatatools can only fetch 30 days data recently, so it dosen't need to specified start and end date\n",
    "    symbol, exchange = extract_vt_symbol(vt_symbol)\n",
    "    rq_symbol = to_rq_symbol(symbol, exchange)\n",
    "\n",
    "    df, msg = futures.get_kline(source_interval, rq_symbol)\n",
    "    df['datetime'] = df['datetime'].map(\n",
    "        lambda x: datetime.strptime(x, '%Y-%m-%d %H:%M:%S'))\n",
    "    df.set_index('datetime', inplace=True, drop=True)\n",
    "    df = df[::-1].copy()\n",
    "\n",
    "    df = handle_df(df, source_interval)\n",
    "#     return df\n",
    "    return df.to_dict(orient=\"records\")\n",
    "\n",
    "\n",
    "def save_all_data(client: RpcClient, source_interval: str, symbols: Optional[Sequence[str]] = None):\n",
    "    if symbols is None:\n",
    "        symbols = get_update_symbol(client)\n",
    "    for vt_symbol in symbols:\n",
    "        data_dict = query_by_symbol(vt_symbol, source_interval)\n",
    "        client.save_to_database(data_dict, vt_symbol, source_interval)\n",
    "        print(f\"{vt_symbol}合约数据保存成功\")\n",
    "\n",
    "\n",
    "setting = get_server_setting()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def update_to_2server():\n",
    "    host_home = setting['host_home']\n",
    "    host_home = '192.168.0.107'\n",
    "    host_tencent = setting['host_cloud']\n",
    "    port = setting['port']\n",
    "    authkey = setting['authkey'].encode('ascii')\n",
    "    source_interval = \"60m\"\n",
    "    \n",
    "    client_home, client_tencent = None, None\n",
    "    \n",
    "    client_home = init_client(host_home, port, authkey)\n",
    "    if client_home:\n",
    "        symbols = get_update_symbol(client_home)\n",
    "#         symbols = ['ni2005.SHFE']\n",
    "        \n",
    "        save_all_data(client_home, source_interval, symbols)\n",
    "        client_home.close()\n",
    "    \n",
    "    client_tencent = init_client(host_tencent, port, authkey)\n",
    "    if client_tencent:\n",
    "        symbols = get_update_symbol(client_tencent)\n",
    "        \n",
    "        save_all_data(client_tencent, source_interval, symbols)\n",
    "        client_tencent.close()\n",
    "\n",
    "    return client_home, client_tencent\n",
    "    \n",
    "client_h, client_t = update_to_2server()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def test_server():\n",
    "    host_home = setting['host_home']\n",
    "    host_home = '192.168.0.107'\n",
    "    host_tencent = setting['host_cloud']\n",
    "    port = setting['port']\n",
    "    authkey = setting['authkey'].encode('ascii')\n",
    "#     source_interval = \"60m\"\n",
    "    \n",
    "    client_home, client_tencent = None, None\n",
    "    \n",
    "    client_home = init_client(host_home, port, authkey)\n",
    "    if client_home:\n",
    "        symbols = get_update_symbol(client_home)\n",
    "#         symbols = ['ni2005.SHFE']\n",
    "#         save_all_data(client_home, source_interval, symbols)\n",
    "        client_home.close()\n",
    "    \n",
    "    client_tencent = init_client(host_tencent, port, authkey)\n",
    "    if client_tencent:\n",
    "        symbols = get_update_symbol(client_tencent)\n",
    "#         save_all_data(client_tencent, source_interval, symbols)\n",
    "        client_tencent.close()\n",
    "\n",
    "    return client_home, client_tencent\n",
    "    \n",
    "client_h, client_t = test_server()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "query_by_symbol('rb2005.SHFE', '60m')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
